/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bll;
import object.Student;
import java.sql.ResultSet;
import java.util.ArrayList;
import dal.HocSinhDAL;
/**
 *
 * @author Le Thai
 */
public class HocSinhBll {
    private ArrayList<Student> students=new ArrayList<>();
    private HocSinhDAL hsDAL=new HocSinhDAL();

    //find student by class
    public StudentModel timHocSinh(String ten,int malop,int manh){
        students.clear();
        ResultSet res=hsDAL.timHocSinh(ten,malop,manh);
        try {
            while(res.next()){                
                //prevent address from null
                String address=res.getString(6);
                if(address==null) address="";        
                //create new student object
                Student std=new Student();
                std.setId(res.getInt(1));
                std.setLastName(res.getString(2));
                std.setFirstName(res.getString(3));
                std.setGender(res.getBoolean(4));
                std.setDayOfBirth(res.getString(5));
                std.setHomeAddress(address);
                std.setCurentClass(res.getString(7));             
                //add new student to arraylist
                students.add(std);
            }
        } catch (Exception e) {
            System.out.println("[HocSinhBll][timHocSinh]"+e.getMessage());
        }
        finally{
            hsDAL.Close();
        }
        return new StudentModel(students);
    }
    
    //add new student
    public int themHocSinh(String hodem,String ten, boolean gt,
                               String ns, String dc, int malop){
        int id=hsDAL.themHocSinh(hodem,ten,gt,ns,dc,malop);
        System.out.println("[HocSinhBll][themHocSinh]"+id);
        hsDAL.Close();
        return id;
    }
    //tim theo ma hs
    public StudentModel timTheoMaHs(int maHs, int maNh){
        ResultSet res=hsDAL.timTheoMaHs(maHs, maNh);
        try {
            while(res.next()){                
                //prevent address from null
                String address=res.getString(6);
                if(address==null) address="";        
                //create new student object
                Student std=new Student();
                std.setId(res.getInt(1));
                std.setLastName(res.getString(2));
                std.setFirstName(res.getString(3));
                std.setGender(res.getBoolean(4));
                std.setDayOfBirth(res.getString(5));
                std.setHomeAddress(address);
                std.setCurentClass(res.getString(7));             
                //add new student to arraylist
                students.add(std);
            }
        } catch (Exception e) {
            System.out.println("[HocSinhBll][timTheoMaHs]"+e.getMessage());
        }
        finally{
            hsDAL.Close();
        }
        return new StudentModel(students);
    }
    
    //xoa hoc sinh
    public boolean xoaHocSinh(int maHs){
        return hsDAL.xoaHocSinh(maHs);
    }
    
    //cap nhat
    public boolean capNhatHosSinh(int maHs,String hodem,String ten, boolean gt,
                                    String ns, String dc, int malop){
        return hsDAL.capNhatHocSinh(maHs, hodem, ten, gt, ns, dc, malop);
    }
}
